package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import domain.Item;

public class ItemDao extends BaseDao{

	/**
	 * @param args
	 */
	public ArrayList select(String sql)throws Exception{
		ArrayList<Item>result =new ArrayList<Item>();
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		conn=getConnection();
		pstmt=conn.prepareStatement(sql);
		rs=pstmt.executeQuery();
		while(rs.next()){
			Item obj=new Item();
			obj.setItemid(rs.getString("itemid"));
			obj.setProductid(rs.getString("productid"));
			obj.setListprice(rs.getString("listprice"));
			obj.setUnitcost(rs.getString("unitcost"));
			obj.setSupplier(rs.getString("supplier"));
			obj.setStatus(rs.getString("status"));
			obj.setAttr1(rs.getString("attr1"));
			obj.setAttr2(rs.getString("attr2"));
			obj.setAttr3(rs.getString("attr3"));
			obj.setAttr4(rs.getString("attr4"));
			obj.setAttr5(rs.getString("attr5"));			
			result.add(obj);
		}
		rs.close();
		pstmt.close();
		conn.close();
		return result;	
	}
	public void insert(Item obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="insert into item values(?,?,?,?,?,?,?,?,?,?,?)";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getItemid());
		ps.setString(2, obj.getProductid());
		ps.setString(3, obj.getListprice());
		ps.setString(4, obj.getUnitcost());
		ps.setString(5, obj.getSupplier());
		ps.setString(6, obj.getStatus());
		ps.setString(7, obj.getAttr1());
		ps.setString(8, obj.getAttr2());
		ps.setString(9, obj.getAttr3());
		ps.setString(10, obj.getAttr4());
		ps.setString(11, obj.getAttr5());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void update(Item obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="update item set productid=?,listprice=?,unitcost=?,supplier=?,status=?,attr1=?,attr2=?,attr3=?,attr4=?,attr5=? where itemid=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getProductid());	
		ps.setString(2, obj.getListprice());
		ps.setString(3, obj.getUnitcost());
		ps.setString(4, obj.getSupplier());	
		ps.setString(5, obj.getStatus());
		ps.setString(6, obj.getAttr1());
		ps.setString(7, obj.getAttr2());
		ps.setString(8, obj.getAttr3());
		ps.setString(9, obj.getAttr4());	
		ps.setString(10, obj.getAttr5());
		ps.setString(11, obj.getItemid());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void delete(Item obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="delete from item where itemid=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getItemid());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	@SuppressWarnings("rawtypes")
	public Item getItem(String ItemId) throws Exception{
		Item obj=null;
		ArrayList list=select("select*from item where itemid='"+ItemId+"'");
		if(list.size()>0){
			obj=(Item) list.get(0);
		}
		return obj;
	}
	public Item[] getItemListByProduct(String productId) throws Exception{
		ArrayList list=select("select*from item where productid='"+productId+"'");
		Item[] obj=new Item[list.size()];
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				obj[i]=(Item) list.get(i);
			}
		}
		return obj;
	}
}
